﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using QuanLyVanBanBUS;

public partial class QLVB_User_VanBanDaDuyet : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void gvVanBanDaDuyet_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gvVanBanDaDuyet.PageIndex = e.NewPageIndex;

        layDanhSachVanBanDaDuyetTheoMaNguoiDung();
    }
    protected void layDanhSachVanBanDaDuyetTheoMaNguoiDung()
    {
        VanBan vbDTO = new VanBan();
        vbDTO.MaNguoiSoanThao = (String)Session["MaNguoiDung"];
        vbDTO.MaNguoiKiemSoat = (String)Session["MaNguoiDung"];

        Session["NgayTu_VanBanDaDuyet"] = txtNgayTu.Text;
        Session["NgayDen_VanBanDaDuyet"] = txtNgayDen.Text;
        string _ngayTu = (String)Session["NgayTu_VanBanDaDuyet"];
        string _ngayDen = (String)Session["NgayDen_VanBanDaDuyet"];

        if (_ngayTu == string.Empty && _ngayDen == string.Empty)
        {
            DateTime ngayTu = new DateTime(1753, 01, 01);

            DateTime ngayDen = new DateTime(9999, 12, 31);

            VanBanBUS vbBUS = new VanBanBUS();
            gvVanBanDaDuyet.DataSource = vbBUS.layDanhSachVanBanDaDuyetTheoMaNguoiDung(vbDTO, ngayTu, ngayDen);
            gvVanBanDaDuyet.DataBind();
        }

        if (_ngayTu != string.Empty && _ngayDen != string.Empty)
        {
            DateTime ngayTu = DateTime.ParseExact(_ngayTu, "dd/MM/yyyy", null);

            DateTime ngayDen = DateTime.ParseExact(_ngayDen, "dd/MM/yyyy", null);

            if (ngayDen.Date >= ngayTu.Date)
            {
                VanBanBUS vbBUS = new VanBanBUS();
                gvVanBanDaDuyet.DataSource = vbBUS.layDanhSachVanBanDaDuyetTheoMaNguoiDung(vbDTO, ngayTu, ngayDen);
                gvVanBanDaDuyet.DataBind();
            }
            else
            {
                Alert.Show("Ngay den khong duoc nho hon Ngay tu");

                return;
            }
        }
    }
    protected void btnTimKiem_Click(object sender, EventArgs e)
    {
        layDanhSachVanBanDaDuyetTheoMaNguoiDung();
    }
    protected void gvVanBanDaDuyet_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        /*Label lblNoiNhan = (Label)(e.Row.FindControl("lblNoiNhan"));
        if (lblNoiNhan != null)
        {
            string sNoiNhan = string.Empty;
            string maVanBan = string.Empty;
            if (e.Row.DataItemIndex >= 0)
            {
                maVanBan = gvVanBanDaDuyet.DataKeys[e.Row.DataItemIndex].Value.ToString();
            }
            List<NoiNhan> lstNoiNhan = (new NoiNhanBUS()).layDanhSachMaNguoiNhanTheoMaVanBan(maVanBan);
            foreach (NoiNhan nn in lstNoiNhan)
            {
                sNoiNhan += nn.MaNguoiNhan.ToString() + "\n"; //Xuong dong
                //sNoiNhan += nn.MaNguoiNhan.ToString() + ",";    //Them dau phay ","
            }
            lblNoiNhan.Text = sNoiNhan;
        }*/

        Label lblNoiNhan = (Label)(e.Row.FindControl("lblNoiNhan"));
        if (lblNoiNhan != null)
        {
            string sNoiNhan = string.Empty;
            string maVanBan = string.Empty;

            maVanBan = gvVanBanDaDuyet.DataKeys[e.Row.RowIndex].Value.ToString();
            List<NoiNhan> lstNoiNhan = (new NoiNhanBUS()).layDanhSachMaNguoiNhanTheoMaVanBanDaDuyet(maVanBan);
            foreach (NoiNhan nn in lstNoiNhan)
            {
                sNoiNhan += nn.MaNguoiNhan.ToString() + "\n"; //Xuong dong
                //sNoiNhan += nn.MaNguoiNhan.ToString() + ",";    //Them dau phay ","
            }
            lblNoiNhan.Text = sNoiNhan;
        }
    }
    protected void btnTroVe_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/pHome.aspx");
    }
}